package com.lks.investor.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.lks.common.service.BaseServiceImpl;
import com.lks.common.util.ConstantFinalUtil;
import com.lks.common.util.PageInfoUtil;
import com.lks.investor.dao.IABankDao;
import com.lks.investor.dao.IAGrantDao;
import com.lks.investor.dao.IAInvestorBankDao;
import com.lks.investor.dao.IAInvestorDao;
import com.lks.investor.pojo.ABank;
import com.lks.investor.pojo.AGrant;
import com.lks.investor.pojo.AInvestor;
import com.lks.investor.pojo.AInvestorBank;
import com.lks.investor.service.IInvestorService;

@Service("investorService")
public class InvestorServiceImpl extends BaseServiceImpl implements IInvestorService
{
	@Resource
	private IABankDao bankDao;

	@Resource
	private IAGrantDao grantDao;

	@Resource
	private IAInvestorDao investorDao;

	@Resource
	private IAInvestorBankDao investorBankDao;

	/*------------------------------------银行表开始-------------------------------------*/
	@Override
	public ABank findOneBankService(Map<String, Object> condMap)
	{
		return this.bankDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneBankService(ABank bank)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.bankDao.insert(bank);
		if (insert > 0)
		{
			resultJSON.put("info", "银行信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", bank.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "银行信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneBankService(ABank bank)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.bankDao.update(bank);
		if (update > 0)
		{
			resultJSON.put("info", "银行信息更新成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", bank.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "银行信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneBankService(ABank bank)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.bankDao.insert(bank);
		if (update > 0)
		{
			resultJSON.put("info", "银行信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", bank.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "银行信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<ABank> findCondListBankService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ABank> bankList = new ArrayList<ABank>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			// 先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			// 查询总记录数
			bankList = this.bankDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(bankList.get(0).getId());

			/* 获取分页数据 */
			condMap.put("pageCount", "false");
			/* 分页信息 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			// 查询的数据
			bankList = this.bankDao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			bankList = this.bankDao.findCondList(condMap);
		}
		return bankList;
	}
	/*------------------------------------银行表结束-------------------------------------*/

	/*------------------------------------授权表开始-------------------------------------*/
	@Override
	public AGrant findOneGrantService(Map<String, Object> condMap)
	{
		return this.grantDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneGrantService(AGrant grant)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.grantDao.insert(grant);
		if (insert > 0)
		{
			resultJSON.put("info", "授权信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", grant.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "授权信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneGrantService(AGrant grant)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.grantDao.update(grant);
		if (update > 0)
		{
			resultJSON.put("info", "授权信息更新成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", grant.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "授权信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGrantService(AGrant grant)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.grantDao.insert(grant);
		if (update > 0)
		{
			resultJSON.put("info", "授权信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", grant.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "授权信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<AGrant> findCondListGrantService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AGrant> grantList = new ArrayList<AGrant>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			// 先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			// 查询总记录数
			grantList = this.grantDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(grantList.get(0).getId());

			/* 获取分页数据 */
			condMap.put("pageCount", "false");
			/* 分页信息 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			// 查询的数据
			grantList = this.grantDao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			grantList = this.grantDao.findCondList(condMap);
		}
		return grantList;
	}
	/*------------------------------------授权表结束-------------------------------------*/

	/*------------------------------------出资的关系表开始-------------------------------------*/
	@Override
	public AInvestor findOneInvestorService(Map<String, Object> condMap)
	{
		return this.investorDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorService(AInvestor investor)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.investorDao.insert(investor);
		if (insert > 0)
		{
			resultJSON.put("info", "出资信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investor.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "出资信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneInvestorService(AInvestor investor, Map<String, Object> paramMap) throws FileNotFoundException, IOException
	{

		JSONObject resultJSON = new JSONObject();
		int update = this.investorDao.update(investor);

		/**
		 * 对其中的file数据进行分析 1.真实的路径 2.其中虚假的路径
		 */
		MultipartFile file = (MultipartFile) paramMap.get("file");

		// 成功的信息
		if (update > 0)
		{
			resultJSON.put("info", "出资信息更新成功");
			resultJSON.put("code", "1");

			/* 原来的图片路径 */
			String oldRelaPath = investor.getLogo();
			/* 文件的原始名字 */
			String originalFilename = file.getOriginalFilename();

			if (file != null && !"".equalsIgnoreCase(file.getOriginalFilename()))
			{
				// 文件的路径
				String realPath = ConstantFinalUtil.BUNDLE.getString("upload.rootFoler") + "/investor/"
						+ this.dateFormatUtil.formatDate(new Date()) + "/" + UUID.randomUUID()
						+ originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
				//真实的路径
				String truePath = ""+paramMap.get("truePath");
				//上传的目标路径
				File targetFile = new File(truePath+realPath);
				
				//目标文件夹目录
				if (!targetFile.exists())
				{
					targetFile.getParentFile().mkdirs();
				}
				
				//拷贝文件
				if (this.fileUtil.copy(file.getInputStream(), new FileOutputStream(targetFile)))
				{
					//赋值图片的路径
					investor.setLogo(realPath);
					this.investorDao.update(investor);
				}
				
				//删除老的图片
				File oldFile = new File(truePath+realPath);
				if (oldFile.exists())
				{
					if(!oldFile.delete())
					{
						ConstantFinalUtil.LOGGER_SYSTEM.info("删除原来图片失败了;出资方id:{},图片路径:{}",investor.getId() , 
								oldFile.getPath());
					}
				}
			}
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investor.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "出资信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneInvestorService(AInvestor investor)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.investorDao.insert(investor);
		if (update > 0)
		{
			resultJSON.put("info", "出资信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investor.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "出资信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<AInvestor> findCondListInvestorService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AInvestor> investorList = new ArrayList<AInvestor>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			// 先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			// 查询总记录数
			investorList = this.investorDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(investorList.get(0).getId());

			/* 获取分页数据 */
			condMap.put("pageCount", "false");
			/* 分页信息 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			// 查询的数据
			investorList = this.investorDao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			investorList = this.investorDao.findCondList(condMap);
		}
		return investorList;
	}
	/*------------------------------------出资关系表结束-------------------------------------*/

	@Override
	public AInvestorBank findOneInvestorBankService(Map<String, Object> condMap)
	{
		return this.investorBankDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorBankService(AInvestorBank investorBank)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.investorBankDao.insert(investorBank);
		if (insert > 0)
		{
			resultJSON.put("info", "银行出资的信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorBank.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "银行出资的信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneInvestorBankService(AInvestorBank investorBank)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.investorBankDao.update(investorBank);
		if (update > 0)
		{
			resultJSON.put("info", "银行出资的信息更新成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorBank.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "银行出资的信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneInvestorBankService(AInvestorBank investorBank)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.investorBankDao.insert(investorBank);
		if (update > 0)
		{
			resultJSON.put("info", "银行出资的信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorBank.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "银行出资的信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<AInvestorBank> findCondListInvestorBankService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AInvestorBank> investorBankList = new ArrayList<AInvestorBank>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			// 先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			// 查询总记录数
			investorBankList = this.investorBankDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(investorBankList.get(0).getId());

			/* 获取分页数据 */
			condMap.put("pageCount", "false");
			/* 分页信息 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			// 查询的数据
			investorBankList = this.investorBankDao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			investorBankList = this.investorBankDao.findCondList(condMap);
		}
		return investorBankList;
	}
}
